<template>
  <div id="app">
    <!-- 顶部导航栏（仅在已登录时显示） -->
    <header v-if="$store.getters.isLogin" class="navbar">
      <div class="container">
        <h2>后台管理系统</h2>
        <div class="user-info">
          <span>欢迎，{{ $store.getters.currentUser?.nickname || '用户' }}</span>
          <button @click="handleLogout" class="logout-btn">登出</button>
        </div>
      </div>
    </header>

    <!-- 页面主体内容 -->
    <main class="main-content">
      <router-view />
    </main>
  </div>
</template>

<script>
import { logout } from '@/api/login'

export default {
  name: 'App',
  methods: {
    async handleLogout() {
      try {
        await logout()
        this.$store.dispatch('logout')
        this.$router.push('/login')
      } catch (err) {
        // 即使登出接口失败，也清除本地状态
        this.$store.dispatch('logout')
        this.$router.push('/login')
      }
    }
  }
}
</script>

<style>
/* 全局样式 */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: Arial, sans-serif;
  background-color: #f5f5f5;
}

#app {
  min-height: 100vh;
}

/* 导航栏样式 */
.navbar {
  background-color: #1890ff;
  color: white;
  padding: 0;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
}

.navbar .container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  height: 60px;
}

.user-info span {
  margin-right: 16px;
}

.logout-btn {
  background: transparent;
  color: white;
  border: 1px solid rgba(255, 255, 255, 0.7);
  padding: 6px 12px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
}

.logout-btn:hover {
  background: rgba(255, 255, 255, 0.1);
}

/* 主体内容区域 */
.main-content {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
}
</style>